package org.csu.small.controller.front;


import org.csu.small.common.Constant;
import org.csu.small.common.ResponseCode;
import org.csu.small.common.UniformResponse;
import org.csu.small.entity.Cart;
import org.csu.small.entity.Favorites;
import org.csu.small.entity.User;
import org.csu.small.service.IFavoritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@RequestMapping("/favorites/")
@Controller
public class FavoritesController {

    @Autowired
    IFavoritesService favoritesService;

    /**
     * 添加产品
     * @param productId
     * @param session
     * @return
     */
    @PostMapping("addFavorites")
    @ResponseBody
    public UniformResponse addFavorites(Integer productId, HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user == null){
            return UniformResponse.createForErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDescription());
        }
        return favoritesService.add(user.getId(),productId);
    }


    /**
     * 移除产品
     * @param productIds
     * @param session
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public UniformResponse delete(String productIds,HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user == null){
            return UniformResponse.createForErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDescription());
        }
        return favoritesService.deleteByProductIds(user.getId(), productIds);
    }

    /**
     * 收藏夹列表
     * @param session
     * @return
     */
    @GetMapping("favList")
    @ResponseBody
    public UniformResponse getFavList(HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user == null){
            return UniformResponse.createForErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDescription());
        }
        return favoritesService.getFavoritesList(user.getId());
    }

    /**
     * 是否在收藏夹中
     * @param session
     * @return
     */
    @GetMapping("isInFav")
    @ResponseBody
    public UniformResponse isInFav(String productId, HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user == null){
            return UniformResponse.createForSuccess(false);
        }
        return favoritesService.isInFav(user.getId(), productId);
    }

    /**
     * 选择收藏夹商品
     * @param productId
     * @param session
     * @return
     */
    @PostMapping("select")
    @ResponseBody
    public UniformResponse select(Integer productId,HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user == null){
            return UniformResponse.createForErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDescription());
        }
        return favoritesService.selectOrUnSelect(user.getId(),productId,Constant.Favorites.CHECKED);
    }

    /**
     * 取消选择
     * @param productId
     * @param session
     * @return
     */
    @PostMapping("unSelect")
    @ResponseBody
    public UniformResponse unSelect(Integer productId,HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user == null){
            return UniformResponse.createForErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDescription());
        }
        return favoritesService.selectOrUnSelect(user.getId(),productId,Constant.Favorites.UN_CHECKED);
    }

    /**
     * 全选
     * @param session
     * @return
     */
    @PostMapping("selectAll")
    @ResponseBody
    public UniformResponse selectAll(HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user == null){
            return UniformResponse.createForErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDescription());
        }
        return favoritesService.selectOrUnSelectAll(user.getId(),Constant.Favorites.CHECKED);
    }


    /**
     * 取消全选
     * @param session
     * @return
     */
    @PostMapping("unSelectAll")
    @ResponseBody
    public UniformResponse unSelectAll(HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user == null){
            return UniformResponse.createForErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDescription());
        }
        return favoritesService.selectOrUnSelectAll(user.getId(),Constant.Favorites.UN_CHECKED);
    }


    /**
     * 获取收藏夹商品数量
     * @param session
     * @return
     */
    @PostMapping("getFavCount")
    @ResponseBody
    public UniformResponse<Integer> getFavCount(HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user == null){
            return UniformResponse.createForErrorCodeMessage(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDescription());
        }
        return favoritesService.getFavCount(user.getId());
    }


}
